//package com.fast.exception;
//
//import com.baomidou.mybatisplus.extension.api.R;
//import com.fast.annotation.NotResponseResult;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.MediaType;
//import org.springframework.http.converter.HttpMessageConverter;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
//
//@Slf4j
//@ControllerAdvice(basePackages = {"com.fast.controller"})
//public class ControllerResponseAdvice implements ResponseBodyAdvice<Object> {
//    @Override
//    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
//        // response是Result类型，或者注释了NotResponseResult都不进行包装
//        return !(methodParameter.getParameterType().isAssignableFrom(R.class)
//                || methodParameter.hasMethodAnnotation(NotResponseResult.class));
//    }
//
//    @Override
//    public Object beforeBodyWrite(Object data, MethodParameter returnType, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest request, ServerHttpResponse response) {
//        // String类型不能直接包装
//        ObjectMapper objectMapper = new ObjectMapper();
//        if (returnType.getGenericParameterType().equals(String.class)) {
//            try {
////                 将数据包装在Result里后转换为json串进行返回
//                return objectMapper.writeValueAsString(R.ok(data));
//            } catch (JsonProcessingException e) {
//                throw new RuntimeException(e.getMessage());
//            }
//        }
//        // 否则直接包装成Result返回
//        return objectMapper;
//    }
//}
